package ml.uuku.oschub.viewpagerfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import ml.uuku.oschub.R;
import ml.uuku.oschub.adpater.ViewPageFragmentAdapter;
import ml.uuku.oschub.base.BaseListFragment;
import ml.uuku.oschub.base.BaseViewPagerFragment;
import ml.uuku.oschub.bean.TweetsList;
import ml.uuku.oschub.fragment.TweetsFragment;
import ml.uuku.oschub.interf.OnTabReselectListener;

/**
 * 动弹界面（包括最新动弹、热门动弹、我的动弹）
 *
 * @author <a href=http://my.oschina.net/uuku>uuku</a>
 * @version V0.1
 */
public class TweetsViewPagerFragment extends BaseViewPagerFragment implements OnTabReselectListener {

    @Override
    protected void onSetupTabAdapter(ViewPageFragmentAdapter adapter) {

        String[] title = getResources().getStringArray(
                R.array.tweets_viewpage_arrays);
        adapter.addTab("new_tweets", title[0], TweetsFragment.class,
                getBundle(TweetsList.CATALOG_LATEST));
        adapter.addTab("hot_tweets", title[1], TweetsFragment.class,
                getBundle(TweetsList.CATALOG_HOT));
        adapter.addTab("my_tweets", title[2], TweetsFragment.class,
                getBundle(TweetsList.CATALOG_ME));
    }

    private Bundle getBundle(int catalog) {
        Bundle bundle = new Bundle();
        bundle.putInt(BaseListFragment.BUNDLE_KEY_CATALOG, catalog);
        return bundle;
    }

    @Override
    public void initData() {
    }

    @Override
    public void onTabReselect() {
        try {
            int currentIndex = mViewPager.getCurrentItem();
            Fragment currentFragment = getChildFragmentManager().getFragments()
                    .get(currentIndex);
            if (currentFragment != null
                    && currentFragment instanceof OnTabReselectListener) {
                OnTabReselectListener listener = (OnTabReselectListener) currentFragment;
                listener.onTabReselect();
            }
        } catch (NullPointerException e) {
        }
    }
}
